package com.jboltai.util.text;

import cn.hutool.core.util.StrUtil;

import java.io.InputStream;

public class FileInfo {
    private String suffix;
    private String fileName;

    private InputStream inputStream;

    public FileInfo() {

    }
    public FileInfo(String fileName, InputStream inputStream) {

        this.fileName = fileName;
        if (StrUtil.isNotBlank(fileName)) {
            this.suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        this.inputStream = inputStream;
    }



    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * 获取文件类型
     *
     * @return
     */
    public FileType getType() {
        if (isImage()) {
            return FileType.IMG;
        } else if (isVideo()) {
            return FileType.VIDEO;
        } else if (isAudio()) {
            return FileType.AUDIO;
        } else {
            return FileType.ATTACHMENT;
        }
    }

    /**
     * 是否是图片
     *
     * @return
     */
    public boolean isImage() {
        return PathUtil.isImage(suffix);
    }

    /**
     * 是否是视频
     *
     * @return
     */
    public boolean isVideo() {
        return PathUtil.isVideo(suffix);
    }

    /**
     * 是否是音频
     *
     * @return
     */
    public boolean isAudio() {
        return PathUtil.isAudio(suffix);
    }

    /**
     * 是否是附件
     *
     * @return
     */
    public boolean isAttachment() {
        return getType() == FileType.ATTACHMENT;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
        if (StrUtil.isNotBlank(fileName)) {
            this.suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
    }
}
